unit Ini;

interface

uses Classes, IniFiles, Controls, Contnrs;

type
  TLinkedItem = class
  public
    FSection: string;
    FIdent: string;
    FControl: pointer;
  end;

  TIni = class
  private
    FIniFile: TIniFile;
    FLinkedItems: TObjectList;
    procedure SaveToIni;
  public
    constructor Create(FileName: string);
    destructor Destroy; override;
    procedure Link(Control: TWinControl; Section, Ident: string);
  end;

implementation
uses TypInfoUtils;
{ TIni }

constructor TIni.Create(FileName: string);
begin
  FIniFile := TIniFile.Create(FileName);
  FLinkedItems := TObjectList.Create;
end;

destructor TIni.Destroy;
begin
  SaveToIni;
  //
  FLinkedItems.Free;
  FIniFile.Free;
  Inherited Destroy;
end;

procedure TIni.Link(Control: TWinControl; Section, Ident: string);
var
  value: string;
  NewItem: TLinkedItem;
begin
  value := FIniFile.ReadString(Section, Ident, '');
  SetStringProperty(Control, 'Text', value);
  //
  NewItem := TLinkedItem.Create;
  NewItem.FSection := Section;
  NewItem.FIdent := Ident;
  NewItem.FControl := Control;
  FLinkedItems.Add(NewItem);
end;

procedure TIni.SaveToIni;
var
  i: integer;
  value: string;
begin
  for i := 0 to FLinkedItems.Count - 1 do
    with TLinkedItem(FLinkedItems[i]) do begin
      value := GetStringProperty(FControl, 'Text');
      FIniFile.WriteString(FSection, FIdent, value);
    end;
end;

end.
